/* 
 * File:   utils.cpp
 * Author: Elias Capito, Jonathan Neuhaus
 * 
 * Created on 6. mai 2010, 15:18
 */

#include "utils.h"

//Permet de dessiner une croix sur l'image en coordonnée x,y
IplImage* Utils::drawCross(int x, int y, IplImage *image, CvScalar color, char *legend) {
    IplImage* tmp = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
    cvCopyImage(image, tmp);
    cvLine(tmp, cvPoint(x - 10, y), cvPoint(x + 10, y), color, 2);
    cvLine(tmp, cvPoint(x, y - 10), cvPoint(x, y + 10), color, 2);

    CvFont font;
    double hScale = 0.5;
    double vScale = 0.5;
    int lineWidth = 1;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);

    cvPutText(tmp, legend, cvPoint(x + 5, y + 15), &font, color);
    return tmp;
}

//Permet de dessiner un cercle sur l'image en coordonnée x,y avec un rayon
IplImage* Utils::drawCircle(int x, int y, int rayon, IplImage *image, CvScalar color, char *legend) {
    IplImage* tmp = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
    cvCopyImage(image, tmp);

    cvCircle(tmp, cvPoint(x, y), rayon, color, 2, 8, 0);

    CvFont font;
    double hScale = 0.5;
    double vScale = 0.5;
    int lineWidth = 1;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);

    cvPutText(tmp, legend, cvPoint(x + 5, y + 15), &font, color);
    return tmp;
}

//Permet de dessiner une ligne sur l'image à partir de deux points
IplImage* Utils::drawLign(int x1, int y1, int x2, int y2, IplImage *image, CvScalar color, char *legend) {
    IplImage* tmp = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
    cvCopyImage(image, tmp);

    cvLine(tmp, cvPoint(x1, y1), cvPoint(x2, y2), color, 2);

    CvFont font;
    double hScale = 0.7;
    double vScale = 0.7;
    int lineWidth = 2;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);

    cvPutText(tmp, legend, cvPoint(x2 + 5, y2 - 15), &font, color);
    return tmp;
}

//Permet de dessiner un texte qui affiche l'heure sur l'horloge
IplImage* Utils::drawTextHour(int x, int y, IplImage *image, CvScalar color, int hour, int min) {
    IplImage* tmp = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
    cvCopyImage(image, tmp);

    char buffer [32];
    sprintf(buffer, "Heure lue: %2d:%02d", hour, min);

    CvFont font;
    double hScale = 1;
    double vScale = 1;
    int lineWidth = 3;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);

    cvPutText(tmp, buffer, cvPoint(x, y), &font, color);
    return tmp;
}

//Permet d'afficher l'histogramme d'une image en niveau de gris
void Utils::showHistoGray(IplImage * img, char *str) {
    IplImage* imgHistogram = 0;

    CvHistogram* hist;
    //size of the histogram -1D histogram
    int bins = 256;
    int hsize[] = {bins};

    //max and min value of the histogram
    float max_value = 0, min_value = 0;

    //value and normalized value
    float value;
    int normalized;

    //ranges - grayscale 0 to 256
    float xranges[] = {0, 256};
    float* ranges[] = {xranges};

    char textOrignal[80];
    char textHisto[80];
    strcpy(textOrignal, "Orignal ");
    strcat(textOrignal, str);

    strcpy(textHisto, "Histo ");
    strcat(textHisto, str);

    //planes to obtain the histogram, in this case just one
    IplImage * planes[] = {img};

    //get the histogram and some info about it
    hist = cvCreateHist(1, hsize, CV_HIST_ARRAY, ranges, 1);
    cvCalcHist(planes, hist, 0, NULL);
    cvGetMinMaxHistValue(hist, &min_value, &max_value);

    //create an 8 bits single channel image to hold the histogram
    //paint it white
    imgHistogram = cvCreateImage(cvSize(bins, 50), 8, 1);
    cvRectangle(imgHistogram, cvPoint(0, 0), cvPoint(256, 50), CV_RGB(255, 255, 255), -1);

    //draw the histogram :P
    for (int i = 0; i < bins; i++) {
        value = cvQueryHistValue_1D(hist, i);
        normalized = cvRound(value * 50 / max_value);
        cvLine(imgHistogram, cvPoint(i, 50), cvPoint(i, 50 - normalized), CV_RGB(0, 0, 0));
    }

    //show the image results
    showImage(img, textOrignal);
    showImage(imgHistogram, textHisto);
}

//Permet de sauvegarder une image
void Utils::saveImage(IplImage* image, char* pathname) {
    cvSaveImage(pathname, image);
}

//Permet d'afficher une image
void Utils::showImage(IplImage* image, char* desc) {
    cvShowImage(desc, image);
}

//Permet de charger une image
IplImage* Utils::loadImage(char* nameFile) {
    return cvLoadImage(nameFile);
}
